package io.coderyeah.ymcc.mq.consumer;

import com.alibaba.fastjson2.JSON;
import io.coderyeah.ymcc.constant.MQConstants;
import io.coderyeah.ymcc.domain.CourseOrder;
import io.coderyeah.ymcc.dto.PayResultMsgInMQ;
import io.coderyeah.ymcc.service.ICourseOrderService;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author lqs
 * @date 2022/11/6 15:38
 */
@Component
@RocketMQMessageListener(topic = MQConstants.MQ_PAY_TOPIC,    //topic
        selectorExpression = MQConstants.MQ_PAY_TAGS,         //tags
        consumerGroup = "service-pay-producer",                     //生产者组名字
        messageModel = MessageModel.BROADCASTING)                 //消息的消费模式 CLUSTERING：集群    BROADCASTING：广播
public class PayResultConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseOrderService courseOrderService;

    @Override
    public void onMessage(MessageExt message) {
        PayResultMsgInMQ res = JSON.parseObject(message.getBody(), PayResultMsgInMQ.class);
        String orderSn = res.getOrderSn();
        // 通过订单号查询订单是否存在
        CourseOrder courseOrder = courseOrderService.getByOrderNo(orderSn);
        if (courseOrder.getStatusOrder() != 1) {
            // 修改订单状态
            courseOrder.setStatusOrder(1);
            courseOrder.setUpdateTime(new Date());
            courseOrderService.updateById(courseOrder);
        }
    }
}
